package com.ssm.order.controller;

import com.ssm.dto.CommonResult;
import com.ssm.model.order.OrderModel;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.ssm.model.stock.StockModel;
import com.ssm.order.exception.SentinelResourceExceptionHandler;
import com.ssm.order.fegin.StockFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
public class OrderController {


    @Autowired
    private StockFeign stockFeign;


    @SentinelResource(value = "ResOrderGet",
            fallback = "fallback",
            fallbackClass = SentinelResourceExceptionHandler.class,
            blockHandler = "blockHandler",
            blockHandlerClass = SentinelResourceExceptionHandler.class
    )
    @GetMapping("/order/get/{id}")
    public CommonResult<StockModel> getStockDetails(@PathVariable Integer id) {
        StockModel stockModel = new StockModel();
        stockModel.setCode("STOCK==>1000");
        stockModel.setId(id);
        return CommonResult.success(stockModel);
    }

    @GetMapping("/getStockDetail")
    public CommonResult<StockModel> getStockDetail() {
        CommonResult<StockModel> result = stockFeign.getStockDetail();
        if (result.getCode() != 1) {
            return CommonResult.error(null, result.getCode(), result.getMessage());
        }
        return result;
    }


    @GetMapping("/getOrderDetailAndStock")
    public CommonResult<OrderModel> hegetOrderDetailAndStockllo() {
        CommonResult<StockModel> result = stockFeign.getStockDetail();
        if (result.getCode() != 1) {
            return CommonResult.error(null, result.getCode(), result.getMessage());
        }
        StockModel stockDetail = result.getData();
        OrderModel orderModel = new OrderModel();
        orderModel.setStockModel(stockDetail);
        return CommonResult.success(orderModel);
    }



    @SentinelResource(value = "GetOrderDetail")
    @GetMapping("/getOrderDetail")
    public OrderModel getOrderDetail() {
        OrderModel orderModel = new OrderModel();
        orderModel.setCode("100-1000-x.y.z");
        orderModel.setId(1);
        return orderModel;
    }





    @PostConstruct
    private static void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();

        FlowRule rule = new FlowRule();
        rule.setResource("test1");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(1);

        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
